package common.dao;
import common.DataExchange;
import common.domain.ExamQuestions;
import java.io.IOException;
import java.util.Set;



/**
 * @author LinLe
 * @create 2020-12-26 10:38
 */
//服务器中对于管理层对考题的操作
public class ExamInformationOperator {
    private Set<ExamQuestions> examQt;
    
    public ExamInformationOperator() {
    }
    
    public ExamInformationOperator(Set<ExamQuestions> examQt) {
        this.examQt = examQt;
    }
    
    public Set<ExamQuestions> getExamQt() {
        return examQt;
    }
    public void add(DataExchange de) throws IOException, ClassNotFoundException {
        ExamQuestions sm = de.receiveExamQuestions();
        boolean add = examQt.add(sm);
        if(add){
            de.sendString("添加成功");
        }else{
            de.sendString("添加失败");
        }
    }
    
    public void delete(DataExchange de) throws IOException, ClassNotFoundException {
        String title = de.receiveString();
        boolean b = examQt.stream().noneMatch(o -> o.getQuestion().equalsIgnoreCase(title));
        if(!b){
            ExamQuestions[] questions = examQt.stream().filter(o -> o.getQuestion().equalsIgnoreCase(title)).toArray(ExamQuestions[]::new);
            examQt.remove(questions[0]);
            de.sendString("删除成功");
        }else{
            de.sendString("该学员不存在");
        }
    }
    
    public void find(DataExchange de) throws IOException, ClassNotFoundException {
        String title = de.receiveString();
        boolean b = examQt.stream().noneMatch(o -> o.getQuestion().equalsIgnoreCase(title));
       
        if(!b){
            ExamQuestions[] questions = examQt.stream().filter(o -> o.getQuestion().equalsIgnoreCase(title)).toArray(ExamQuestions[]::new);
    
            de.sendExamQuestions(questions[0]);
        }else{
            de.sendExamQuestions(null);
        }
    }
    
    public void change(DataExchange de) throws IOException, ClassNotFoundException {
        String title = de.receiveString();
        boolean b = examQt.stream().noneMatch(o -> o.getQuestion().equalsIgnoreCase(title));
        if (!b) {
            ExamQuestions[] array = examQt.stream().filter(o -> o.getQuestion().equalsIgnoreCase(title)).toArray(ExamQuestions[]::new);
            de.sendExamQuestions(array[0]);
            examQt.remove(array[0]);
            ExamQuestions questions = de.receiveExamQuestions();
            if(examQt.add(questions)) {
                de.sendString("修改成功");
            }else{
                de.sendString("已近有了");
            }
        }else{
            de.sendString(null);
        }
        
    }
    
    public void show(DataExchange de) throws IOException {
        if(examQt.isEmpty()){
            de.sendString("还没有题目");
        }else{
            de.sendString("有题目了");
            de.sendSet(examQt);
            
        }
    }
}
